package com.online.pay.common.utils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 生成随机数工具类
 * @author Qiurz
 *
 */
public class RandomNumberUtil {
	
	private static final String DEFULT_TANSID_TYPE = "10";
	private static final String DEFULT_TANSID_PROCODE = "999";
	
	private static final int TRADE_ORDERNO = 32;
	
	private static final char[] mm = new char[]{'0','1','2','3','4','5','6','7','8','9'};

	private static final Random RANDOM = new SecureRandom();
	
	private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>(){
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyyMMddHHmmssSSS");
		}
	};
	
	/**
	 * 生成自定义长度的随机数序列
	 * @param len
	 * @return
	 */
	public static String genrateRandomSerial(int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			sb.append(mm[RANDOM.nextInt(mm.length)]);
		}
		return sb.toString();
	}
	
	/**
	 * 获取随机订单号
	 * @return
	 */
	public static String getTradeOderNo() {
		StringBuilder sb = new StringBuilder();
		sb.append(local.get().format(new Date())).append(DEFULT_TANSID_TYPE)
			.append(DEFULT_TANSID_PROCODE).append(genrateRandomSerial(TRADE_ORDERNO-sb.length()));
		return sb.toString();
	}
	
}
